# -*- coding: utf-8 -*-

import requests
import re

from sina_数据 import showChart
import numpy as np


def get_data():
    # 爬取新浪财经的数据
    # 默认是15min

    url = 'https://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/var%20_fx_seurusd_15_1570801701818=/NewForexService.getMinKline?symbol=fx_seurusd&scale=15&datalen=480'
    url = 'https://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/var%20_fx_seurusd_5_1570801701818=/NewForexService.getMinKline?symbol=fx_seurusd&scale=15&datalen=480'
    """'
    https://vip.stock.finance.sina.com.cn/forex/api/jsonp.php/
    var%20_fx_seurusd_15_1570801701818=
    var%20_fx_seurusd_15_1570801701818=
    var%20_fx_seurusd_15_1570804828369
    var%20_fx_seurusd_15_1570805166290
    '

    """
    headers = {}

    response = requests.get(url=url)

    # print(response.text)

    data = response.text

    data = re.findall('=\((.*?)\)', data)[0]
    data = data.replace('"', "").replace("d:", "'d':'").replace(',o', "','o'").replace("l", "'l'").replace('c',
                                                                                                           "'c'").replace(
        'h',
        "'h'")
    data = eval(data)
    # print(data)
    return data


def StatisticsDistanceMA():
    # 统计距离MA均线的距离
    data = get_data()
    MA_period = 60

    # print(data[6:95])

    # 所有距离MA的值的集合
    MA_collect = []

    for i in range(len(data)):
        if i < MA_period: continue
        now = float(data[i]['c'])
        MA = sum([float(n['c']) for n in data[i - MA_period:i]]) / MA_period
        print('当前MA为：', MA, "\n当前值为：", now)
        print('当前距离MA的值为：', now - MA)
        MA_collect.append(abs(now - MA))
    # print(MA_collect)
    print('当前的的数据条目共有：', len(data))

    showChart.showHistogram_x(MA_collect)
    return MA_collect


def MAKIntersectProid():
    # 与均线相交的周期分析
    data = get_data()
    MA_period = 60
    上一次相交 = 0
    与MA上一次相交差值的集合 = []
    for i in range(len(data)):

        now = data[i]
        MA = sum([float(n['c']) for n in data[i - MA_period:i]]) / MA_period

        if now['h'] > MA and now['l'] < MA:
            # print('当前相交')
            # print('当前相交的时间为', now['d'])
            上一次相交 = i
        # print('上一次相交的价格是', data[上一次相交])
        print('当前价位距离上一次相交的差值', now['c'] - data[上一次相交]['c'])
        与MA上一次相交差值的集合.append(now['c'] - data[上一次相交]['c'])

    print(与MA上一次相交差值的集合)
    showChart.showHistogram_x(与MA上一次相交差值的集合)

def 方差():
    # 与均线相交的周期分析
    data = get_data()
    period = 60

    for i in range(len(data)):

        now = data[i]
        MA = sum([float(n['c']) for n in data[i -  period:i]]) / period

        if now['h'] > MA and now['l'] < MA:
            # print('当前相交')
            # print('当前相交的时间为', now['d'])
            上一次相交 = i
        # print('上一次相交的价格是', data[上一次相交])
        print('当前价位距离上一次相交的差值', now['c'] - data[上一次相交]['c'])

    print( )
    showChart.showHistogram_x( )

def 在MA偏离周期内的最高值():
    # 与均线相交的周期分析
    data = get_data()
    MA_period = 60


    for i in range(len(data)):
        now = data[i]
        MA = sum([float(n['c']) for n in data[i - MA_period:i]]) / MA_period
        if now['h'] > MA and now['l'] < MA:
            # print('当前相交')
            # print('当前相交的时间为', now['d'])
            上一次相交 = i


    print( )
    showChart.showHistogram_x( )


if __name__ == '__main__':
    # 小数点后5位
    # get_data()
    # StatisticsDistanceMA()
    MAKIntersectProid()

    # print(get_data())
    # print(len(get_data()))
    # std()
    # 方差()
